/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
package com.baselet.element.relation.facet;

import java.util.HashMap;
import java.util.Map;

import com.baselet.control.basics.geom.Point;
import com.baselet.diagram.draw.DrawHandler;
import com.baselet.diagram.draw.helper.StyleException;
import com.baselet.element.facet.KeyValueFacet;
import com.baselet.element.facet.PropertiesParserState;
import com.baselet.element.relation.helper.LineDescriptionEnum;

public class DescriptionPositionFacet extends KeyValueFacet {

	private static final int MAX_DISP = 200;

	public static final String POS = "pos";

	public static final DescriptionPositionFacet INSTANCE_MESSAGE_START = new DescriptionPositionFacet(LineDescriptionEnum.MESSAGE_START);
	public static final DescriptionPositionFacet INSTANCE_MESSAGE_END = new DescriptionPositionFacet(LineDescriptionEnum.MESSAGE_END);
	public static final DescriptionPositionFacet INSTANCE_ROLE_START = new DescriptionPositionFacet(LineDescriptionEnum.ROLE_START);
	public static final DescriptionPositionFacet INSTANCE_ROLE_END = new DescriptionPositionFacet(LineDescriptionEnum.ROLE_END);

	private final LineDescriptionEnum lineDesc;

	public DescriptionPositionFacet(LineDescriptionEnum lineDesc) {
		super();
		this.lineDesc = lineDesc;
	}

	@Override
	public KeyValue getKeyValue() {
		return new KeyValue(lineDesc.getKey() + POS, false, "-5,7", "comma separated integers as displacement of " + lineDesc + " text (first=horizontal, second=vertical)");
	}

	@Override
	public void handleValue(String value, DrawHandler drawer, PropertiesParserState state) {
		try {
			Map<String, Point> displacements = state.getOrInitFacetResponse(DescriptionPositionFacet.class, new HashMap<String, Point>());
			String[] split = value.split(",");
			int x = Integer.parseInt(split[0]);
			int y = Integer.parseInt(split[1]);
			if (x > MAX_DISP || y > MAX_DISP) {
				throw new StyleException("max allowed displacement value is " + MAX_DISP);
			}
			displacements.put(lineDesc.getKey(), new Point(x, y));
		} catch (Exception e) {
			if (e instanceof StyleException) {
				throw (StyleException) e;
			}
			throw new StyleException("value must be <integer>,<integer>");
		}

	}

}
